home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / AmigaMesa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-25  |  13.5 KB  |  571 lines

  1. /* $Id: AmigaMesa.c 1.16 1997/06/25 19:16:56 StefanZ Exp StefanZ $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Library General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  * Library General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Library General Public
  18.  * License along with this library; if not, write to the Free
  19.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. /*
  23. $Log: AmigaMesa.c $
  24.  * Revision 1.16  1997/06/25  19:16:56  StefanZ
  25.  * New drawing rutines:
  26.  * - Now in separate files
  27.  * - cyberGfx added.
  28.  *
  29.  * Revision 1.15  1996/10/13  20:54:59  StefanZ
  30.  * A few nasty bugfixes.
  31.  *
  32.  * Revision 1.14  1996/10/07  00:11:07  StefanZ
  33.  * Mesa 2.0 Fixed
  34.  *
  35.  * Revision 1.13  1996/10/06  20:31:50  StefanZ
  36.  * Source Bump before Mesa 2.0
  37.  *
  38.  * Revision 1.12  1996/08/14  22:17:32  StefanZ
  39.  * New API to amigacalls (uses taglist)
  40.  * Made it more flexible to add gfx-card support.
  41.  * Fast doublebuff rutines
  42.  * minor bugfixes
  43.  *
  44.  * Revision 1.10    1996/06/12  13:06:00  StefanZ
  45.  * BugFix and encatments by Jorge Acereda (JAM)
  46.  *
  47.  * Revision 1.9  1996/06/11  15:53:02   StefanZ
  48.  * Little speedup, VerryFast colorallocation by Stefan Burstöm
  49.  *
  50.  * Revision 1.8  1996/06/01  23:57:47   StefanZ
  51.  * Started to use RCS to keep track of code.
  52.  *
  53.  * Revision 1.7  1996/05/21  23:08:42   StefanZ
  54.  * A few bug and enforcer fixes
  55.  *
  56.  * Revision 1.6  1996/04/29  22:14:31   StefanZ
  57.  * BugFixes reported by by Daniel Jönsson
  58.  *
  59.  * Revision 1.5  1996/03/14  23:54:33   StefanZ
  60.  * Doublebuffer & Tmprastport seams to work (big speed improvment)
  61.  * a fastpolydraw is also implemented
  62.  *
  63.  * Revision 1.4  1996/03/07  16:55:04   StefanZ
  64.  * Much of the code works now (RGB mode is simulated) Doublebuffers... (didn't work)
  65.  *
  66.  * Revision 1.3  1996/02/29  02:12:45   StefanZ
  67.  * First sight of colors (even the right ones) maglight.c works
  68.  *
  69.  * Revision 1.2  1996/02/25  13:11:16   StefanZ
  70.  * First working version. Draws everything with the same color
  71.  * (Colormaping is now urgent needed)
  72.  *
  73.  * Revision 1.1  1996/02/23  22:01:15   StefanZ
  74.  * Made changes to match latest version of ddsample 1.5
  75.  *
  76.  * Revision 1.0  1996/02/21  11:01:15   StefanZ
  77.  * File created from ddsample.c ver 1.3 and amesa.c ver 1.5
  78.  * in a brave atempt to rebuild the amiga version
  79.  *
  80.  */
  81.  
  82.  
  83. /*
  84. TODO:
  85. Dynamic allocate the vectorbuffer for polydrawing. (memory improvment)
  86. implement shared list.
  87. fix resizing bug.
  88. some native asm rutine
  89. fast asm line drawin in db mode
  90. fast asm clear       in db mode
  91. read buffer rutines  in db-mode
  92.  
  93. IDEAS:
  94.  Make the gl a sharedlibrary. (Have ben started look in /amiga)
  95. */
  96.  
  97.  
  98. /*
  99.  * Note that you'll usually have to flip Y coordinates since Mesa's
  100.  * window coordinates start at the bottom and increase upward.  Most
  101.  * window system's Y-axis increases downward
  102.  *
  103.  * See dd.h for more device driver info.
  104.  * See the other device driver implementations for ideas.
  105.  *
  106.  */
  107.  
  108.  
  109. #include <exec/memory.h>
  110. #include <exec/types.h>
  111. #ifdef __GNUC__
  112. #include <inline/exec.h>
  113. #include <inline/intuition.h>
  114. #include <inline/graphics.h>
  115. #include <inline/utility.h>
  116. #else
  117. #include <proto/exec.h>
  118. #include <proto/intuition.h>
  119. #include <proto/graphics.h>
  120. #include <proto/utility.h>
  121. #endif
  122.  
  123. #ifdef ADISP_CYBERGFX
  124. #ifdef __GNUC__
  125. #include <proto/cybergraphics.h>
  126. #else
  127. #include <inline/cybergraphics.h>
  128. #endif
  129. #endif
  130.  
  131. #include <gl/AmigaMesa.h>
  132.  
  133. #ifdef ADISP_CYBERGFX
  134. #include "ADisp_Cyb.h"
  135. #endif
  136.  
  137. #ifdef ADISP_AGA
  138. #include "ADisp_AGA.h"
  139. #endif
  140.  
  141.  
  142. #include <stdlib.h>
  143. #include <stdio.h>
  144. #include <gl/gl.h>
  145. #include "context.h"
  146. #include "dd.h"
  147. #include "xform.h"
  148. #include "macros.h"
  149. #include "vb.h"
  150.  
  151.  
  152. //#define DEBUGPRINT
  153.  
  154. #ifdef DEBUGPRINT
  155. #define DEBUGOUT(x) printf(x);
  156. #else
  157. #define DEBUGOUT(x) /*printf(x);*/
  158. #endif
  159.  
  160. /**********************************************************************/
  161. /*****                Internal Data                                         *****/
  162. /**********************************************************************/
  163. //struct amigamesa_context * amesa = NULL;
  164. GLenum LastError;                /* The last error generated*/
  165. struct Library *CyberGfxBase = NULL; /* optional base address for cybergfx */
  166.  
  167. /**********************************************************************/
  168. /*****                Some Usefull code                                     *****/
  169. /**********************************************************************/
  170.  
  171. /*
  172.     The Drawing area is defined by:
  173.  
  174.     CC.Viewport.X = x;
  175.     CC.Viewport.Width = width;
  176.     CC.Viewport.Y = y;
  177.     CC.Viewport.Height = height;
  178. */
  179.  
  180.  
  181.  
  182. /**********************************************************************/
  183. /**********************************************************************/
  184.  
  185. //extern void cyb_Faster_DD_pointers( GLcontext *ctx);
  186. //extern void cyb_standard_DD_pointers( GLcontext *ctx);
  187.  
  188.  
  189. /*
  190. long _STI_200_InitMesaLibrary(void)
  191.     {
  192.     if(!GfxBase)
  193.         {
  194. //  printf("Opening graphics.library\n");
  195.         if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",39)))
  196.             {
  197.             printf("MesaOpenGL Error\nCouldent open graphics.library v39\n");
  198.             return(FALSE);
  199.             }
  200.         }
  201. // printf("GfxBase=0x%x\n",GfxBase);
  202.     return(TRUE);
  203.     }
  204.  
  205. void _STD_200_DisposeMesaLibrary(void)
  206.     {
  207.     if (GfxBase)    // @@@ TODO Open and close gfxlibrary when open and flush lib.
  208.         CloseLibrary((struct Library *)GfxBase);
  209.     GfxBase=NULL;
  210.     }
  211. */
  212.  
  213. /**********************************************************************/
  214. /*****                  Amiga/Mesa API Functions                  *****/
  215. /**********************************************************************/
  216. /*
  217.  * Implement the client-visible Amiga/Mesa interface functions defined
  218.  * in Mesa/include/GL/Amigamesa.h
  219.  *
  220.  **********************************************************************/
  221. /*
  222.  * Implement the public Amiga/Mesa interface functions defined
  223.  * in Mesa/include/GL/AmigaMesa.h
  224.  */
  225.  
  226. struct amigamesa_visual *
  227. AmigaMesaCreateVisualTags(long Tag1, ...)
  228. {
  229.     return AmigaMesaCreateVisual(&Tag1);
  230. }
  231.  
  232. #ifdef __GNUC__
  233. struct amigamesa_visual *
  234. AmigaMesaCreateVisual(register struct TagItem *tagList)
  235. #else
  236. __asm __saveds struct amigamesa_visual *
  237. AmigaMesaCreateVisual(register __a0 struct TagItem *tagList)
  238. #endif
  239. {
  240.    struct amigamesa_visual *v;
  241.    int  index_bits;
  242.    GLfloat redscale, greenscale, bluescale, alphascale;;
  243.  
  244.    v = (struct amigamesa_visual *)AllocVec(sizeof(struct amigamesa_visual),MEMF_PUBLIC|MEMF_CLEAR);
  245.    if (!v) {
  246.       return NULL;
  247.    }
  248.  
  249.     v->rgb_flag=GetTagData(AMA_RastPort,GL_TRUE,tagList);
  250.     v->db_flag=GetTagData(AMA_DoubleBuf,GL_FALSE,tagList);
  251.     v->alpha_flag=GetTagData(AMA_AlphaFlag,GL_FALSE,tagList);
  252.  
  253.  
  254.    if (v->rgb_flag) {
  255.       /* RGB(A) mode */
  256.       redscale =   greenscale = bluescale = alphascale = 255;
  257.       index_bits = 0;
  258.    }
  259.    else {
  260.       /* color index mode */
  261.       redscale = 0.0;
  262.       greenscale = 0.0;
  263.       bluescale = 0.0;
  264.       alphascale = 0.0;
  265.       index_bits = 8;           /* @@@ TODO */
  266.    }
  267.  
  268.    /* Create core visual */
  269.    v->gl_visual = gl_create_visual( v->rgb_flag, 
  270.                                     v->alpha_flag,
  271.                                     v->db_flag,
  272.                                                 16,        /* depth_size */
  273.                                                 8,         /* stencil_size */
  274.                                                 16,        /* accum_size */
  275.                                     redscale, greenscale,
  276.                                     bluescale, alphascale,
  277.                                     index_bits );
  278.  
  279.    return v;
  280. }
  281.  
  282. void AmigaMesaDestroyVisual( struct amigamesa_visual *v )
  283. {
  284.    gl_destroy_visual( v->gl_visual );
  285.    FreeVec( v );
  286. }
  287.  
  288.  
  289.  
  290.  
  291. struct amigamesa_buffer *AmigaMesaCreateBuffer( struct amigamesa_visual *visual,int windowid)
  292. {
  293.    struct amigamesa_buffer *b;
  294.  
  295.    b = (struct amigamesa_buffer *) AllocVec(sizeof(struct amigamesa_buffer),MEMF_PUBLIC|MEMF_CLEAR);
  296.    if (!b) {
  297.       return NULL;
  298.    }
  299.  
  300.    b->gl_buffer = gl_create_framebuffer( visual->gl_visual);
  301.  
  302.    /* other stuff */
  303.  
  304.    return b;
  305. }
  306.  
  307.  
  308.  
  309. void AmigaMesaDestroyBuffer( struct amigamesa_buffer *b )
  310. {
  311.     gl_destroy_framebuffer( b->gl_buffer );
  312.    FreeVec( b );
  313. }
  314.  
  315.  
  316. struct amigamesa_context *AmigaMesaCreateContextTags(long Tag1, ...)
  317. {
  318.     return AmigaMesaCreateContext(&Tag1);
  319. }
  320.  
  321. #ifdef __GNUC__
  322. struct amigamesa_context *
  323. AmigaMesaCreateContext(register struct TagItem *tagList)
  324. #else
  325. __asm __saveds struct amigamesa_context *
  326. AmigaMesaCreateContext(register __a0 struct TagItem *tagList)
  327. #endif
  328. {
  329.     /* Create a new Amiga/Mesa context */
  330.     /* Be sure to initialize the following in the core Mesa context: */
  331.     /* DrawBuffer, ReadBuffer */    /* @@@ IMPLEMENTERA ???*/
  332.  
  333.  
  334. //  GLfloat redscale,greenscale,bluescale,alphascale;
  335. //  int         I;
  336. //    int drawMode;
  337.     struct amigamesa_context *c;
  338.  
  339.     /* try to open cybergraphics.library */
  340.     if (CyberGfxBase==NULL)
  341.         {
  342.             CyberGfxBase = OpenLibrary((UBYTE*)"cybergraphics.library",0);
  343.         }
  344.  
  345.         /* allocate amigamesa_context struct initialized to zeros */
  346.     c = (struct amigamesa_context *) AllocVec(sizeof(struct amigamesa_context),MEMF_PUBLIC|MEMF_CLEAR);
  347.     if (!c)
  348.         {
  349.         LastError=AMESA_OUT_OF_MEM;
  350.         return(NULL);
  351.         }
  352.  
  353.  
  354.     c->visual=(struct amigamesa_visual *)GetTagData(AMA_Visual,NULL,tagList);
  355.     c->buffer=(struct amigamesa_buffer *)GetTagData(AMA_Buffer,NULL,tagList);
  356.  
  357.     if(!c->visual)
  358.         {
  359.         if (!(c->visual=AmigaMesaCreateVisual(tagList)))
  360.             {
  361.             LastError=AMESA_OUT_OF_MEM;
  362.             return NULL;
  363.             }
  364.         c->flags=c->flags||0x1;
  365.         }
  366.  
  367.     if(!c->buffer)
  368.         {
  369.         if(!(c->buffer=AmigaMesaCreateBuffer( c->visual,GetTagData(AMA_WindowID,1,tagList))))
  370.             {
  371.             LastError=AMESA_OUT_OF_MEM;
  372.             return NULL;
  373.             }
  374.         c->flags=c->flags||0x2;
  375.         }
  376.  
  377.  
  378.     c->share=(struct amigamesa_context *)GetTagData(AMA_ShareGLContext,NULL,tagList);
  379. //  sharetmp=c->share;
  380.     c->gl_ctx = gl_create_context(  c->visual->gl_visual,
  381.                                                 NULL,
  382. /*                                              sharetmp ? sharetmp->gl_ctx : NULL,*/    /* TODO */
  383.                                                 (void *) c  );
  384.  
  385. //    drawMode=GetTagData(AMA_DrawMode,AMESA_AGA,tagList); 
  386.     if(c->visual->db_flag==GL_TRUE)
  387.         {
  388. DEBUGOUT("This is doubelbuffered\n")
  389. #ifdef ADISP_CYBERGFX
  390.         if (CyberGfxBase)
  391.             {
  392.             if (Cyb_Standard_init_db(c,tagList))
  393.                 return c;
  394.             else
  395.                 {
  396.                 gl_destroy_context( c->gl_ctx );
  397.                 FreeVec( c );
  398.                 return NULL;
  399.                 }
  400. #ifdef ADISP_AGA
  401.             }
  402.         else
  403.             {
  404. #endif
  405. #endif
  406. #ifdef ADISP_AGA
  407. //            if (Amiga_Standard_init_db(c,tagList))   Not realy finishid
  408.             if (Amiga_Standard_init(c,tagList))
  409.                 return c;
  410.             else
  411.                 {
  412.                 gl_destroy_context( c->gl_ctx );
  413.                 FreeVec( c );
  414.                 return NULL;
  415.                 }
  416. #endif
  417. #ifdef ADISP_CYBERGFX
  418.             }
  419. #endif
  420.         }
  421.     else  /* allways fallback on AGA when unknown drawmode */
  422.         {
  423. DEBUGOUT("This is NOT doubelbuffered\n")
  424. #ifdef ADISP_CYBERGFX
  425.         if (CyberGfxBase)
  426.             {
  427.             if (Cyb_Standard_init(c,tagList))
  428.                 return c;
  429.             else
  430.                 {
  431.                 gl_destroy_context( c->gl_ctx );
  432.                 FreeVec( c );
  433.                 return NULL;
  434.                 }
  435. #ifdef ADISP_AGA
  436.             }
  437.         else
  438.             {
  439. #endif
  440. #endif
  441. #ifdef ADISP_AGA
  442.  
  443.             if (Amiga_Standard_init(c,tagList))   /* Add CyberGfx init here also (if ...)*/
  444.                 return c;
  445.             else
  446.                 {
  447.                 gl_destroy_context( c->gl_ctx );
  448.                 FreeVec( c );
  449.                 return NULL;
  450.                 }
  451. #endif
  452. #ifdef ADISP_CYBERGFX
  453.             }
  454. #endif
  455.         }
  456. }
  457.  
  458.  
  459. #ifdef __GNUC__
  460. void AmigaMesaDestroyContext(register struct amigamesa_context *c )
  461. #else
  462. __asm __saveds void AmigaMesaDestroyContext(register __a0 struct amigamesa_context *c )
  463. #endif
  464. {
  465.     /* destroy a Amiga/Mesa context */
  466.  
  467. /*
  468.     if (c==amesa)
  469.         amesa=NULL;
  470. */
  471.     (*c->Dispose)( c );
  472.  
  473.     if(c->flags&&0x1)
  474.         AmigaMesaDestroyVisual(c->visual);
  475.     if(c->flags&&0x2)
  476.         AmigaMesaDestroyBuffer(c->buffer);
  477.     
  478.     gl_destroy_context( c->gl_ctx );
  479.     FreeVec( c );
  480. }
  481.  
  482.  
  483. #ifdef __GNUC__
  484. void AmigaMesaMakeCurrent(register struct amigamesa_context *amesa,register struct amigamesa_buffer *b )
  485. #else
  486. __asm __saveds void AmigaMesaMakeCurrent(register __a0 struct amigamesa_context *amesa,register __a1    struct amigamesa_buffer *b )
  487. #endif
  488. {
  489.     /* Make the specified context the current one */
  490.     /* the order of operations here is very important! */
  491.  
  492. //   Current = amesa;
  493.  
  494.    if (amesa && b) {
  495. //printf("amesa->gl_ctx=0x%x\n",amesa->gl_ctx);
  496.        gl_make_current( amesa->gl_ctx,b->gl_buffer );
  497. //printf("CC=0x%x\n",CC);
  498.  
  499.         (*amesa->InitDD)(amesa->gl_ctx);                            /* Call Driver_init_rutine */
  500.  
  501.  
  502.       if (amesa->gl_ctx->Viewport.Width==0) {
  503.          /* initialize viewport to window size */
  504. //         gl_viewport( amesa->gl_ctx, 0, 0, amesa->width, amesa->height );
  505. DEBUGOUT("glViewport");
  506.             glViewport( 0, 0, amesa->width, amesa->height );
  507. DEBUGOUT("glViewport");
  508.       }
  509.     else
  510.         {
  511.        gl_make_current( NULL,NULL);
  512.         }
  513.    }
  514. }
  515.  
  516.  
  517.  
  518. #ifdef __GNUC__
  519. void AmigaMesaSwapBuffers(register struct amigamesa_context *amesa)
  520. #else
  521. __asm __saveds void AmigaMesaSwapBuffers(register __a0 struct amigamesa_context *amesa)
  522. #endif
  523.     {                /* copy/swap back buffer to front if applicable */
  524.     (*amesa->SwapBuffer)( amesa );
  525.     }
  526.  
  527.  
  528.  
  529.  
  530. /* This is on the drawingboard */
  531. /* Mostly for when future changes the library is still intact*/
  532.  
  533. #ifdef __GNUC__
  534. BOOL AmigaMesaSetDefs(register struct TagItem *tagList)
  535. #else
  536. __asm __saveds BOOL AmigaMesaSetDefs(register __a0 struct TagItem *tagList)
  537. #endif
  538.     {
  539. /*
  540.     struct TagItem *tag;
  541.         
  542.     tagValue=AMA_DrawMode;
  543.     tag = FindTagItem(tagValue,tagList);
  544.     if (tag)
  545.         printf("Tag=0x%x, is 0x%x/n",tagValue,tag->ti_Data);
  546.     else
  547.         printf("Tag=0x%x is not specified/n",tagValue);
  548. */
  549.     return FALSE;
  550.     }
  551.  
  552.  
  553. /*
  554.  Maybe a report error rutine ??? like:
  555. */
  556.  
  557. #ifdef __GNUC__
  558. GLenum AmigaMesaReportError(register struct amigamesa_context *c )
  559. #else
  560. __asm __saveds GLenum AmigaMesaReportError(register __a0 struct amigamesa_context *c )
  561. #endif
  562.     {
  563.     GLenum error;
  564.     error=LastError;
  565.     LastError=NULL;
  566.     return(error);
  567.     }
  568.  
  569.  
  570. #undef DEBUGPRINT
  571.